package com.data;

import com.utils.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HitCourseDao {
    public List<HitCourse> display(int uId){
        List<HitCourse> hitcourses=new ArrayList<>();
        Map<String, Object> map = null;
        try {
            String sql="select uId,uName,cId,cName,score from HitCourse natural join User natural join Course where uId=?";
            Object[] o=new Object[1];
            o[0]=uId;
            List<Map<String, Object>> list = DBUtil.executeQuery(sql,o);
            int size = list.size();
            for(int i=0;i<size;i++){
                map = list.get(i);
                HitCourse hitcourse  = new HitCourse(((Integer)map.get("uId")).intValue(),
                        map.get("uName").toString(),
                        ((Integer)map.get("cId")).intValue(),
                        map.get("cName").toString(),
                        ((Integer)map.get("score")).intValue()
                );
                hitcourses.add(hitcourse);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return hitcourses;
    }
    public void add(int uId,int cId){
        Map<String, Object> value = new HashMap<>();
        List<Map<String, Object>> list=new ArrayList<>();

        Map<String, Object> where = new HashMap<>();
        where.put("cId",cId);

        Map<String, Object> valueMap = new HashMap<>();
        valueMap.put("uId", uId);
        valueMap.put("cId", cId);
        valueMap.put("score", 0);
        try {
            list=DBUtil.query("course",where);
            DBUtil.insert("HitCourse", valueMap);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int cNum=((Integer)list.get(0).get("cNum")).intValue()+1;
        value.put("cNum",cNum);
        try {
            DBUtil.update("course",value,where);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void delete(int uId,int cId){
        Map<String, Object> whereMap = new HashMap<>();
        Map<String, Object> where = new HashMap<>();
        List<Map<String, Object>> list=new ArrayList<>();
        whereMap.put("cId", cId);
        whereMap.put("uId",uId);
        where.put("cId", cId);
        try {
            DBUtil.delete("HitCourse", whereMap);
            list=DBUtil.query("course",where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int cNum=((Integer)list.get(0).get("cNum")).intValue()-1;
        Map<String, Object> valuemap = new HashMap<>();
        valuemap.put("cNum",cNum);
        try {
            DBUtil.update("course",valuemap,whereMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public boolean checkcid(int uid,int cid){
        List<Map<String, Object>> list=null;
        Map<String, Object> wheremap=new HashMap<>();
        wheremap.put("cId",cid);
        wheremap.put("uId",uid);
        try {
            list = DBUtil.query("HitCourse", wheremap);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        if(list.isEmpty()){
            return false;
        }
        return true;
    }
}
